package com.zryc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.service.IOrderDetailService;
import com.zryc.service.IOrderMasterService;
import com.zryc.service.remote.ICustomerAddrService;
import com.zryc.shop.entity.Order;
import com.zryc.shop.entity.OrderDetail;
import com.zryc.shop.entity.OrderMaster;
import com.zryc.shop.utils.Result;
import io.seata.common.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 订单主表 前端控制器
 * </p>
 *
 * @author xujiajun
 * @since 2022-10-17
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单信息管理")
public class OrderMasterController {
    @Autowired
    private IOrderMasterService orderMasterService;
    @Autowired
    private ICustomerAddrService customerAddrService;
    @Autowired
    private IOrderDetailService orderDetailService;

    @GetMapping("/list")
    @ApiOperation("订单列表")
    public Result list( @ApiParam(value = "订单编号") @RequestParam(required = false) String orderSn,
                        @ApiParam(value = "收货人信息（姓名或手机号）")  @RequestParam(required = false) String receiverKeyword,
                        @ApiParam(value = "订单创建时间") @RequestParam(required = false) String createTime,
                        @ApiParam(value = "订单状态")  @RequestParam(required = false) Integer status,
                        @ApiParam(value = "分页页号",example = "1") @RequestParam(defaultValue = "1") Integer pageNum,
                        @ApiParam(value = "分页大小",example = "5") @RequestParam(defaultValue = "5") Integer pageSize){
        QueryWrapper<OrderMaster> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotBlank(orderSn)){
            wrapper.eq("order_sn",orderSn);
        }
        if (StringUtils.isNotBlank(receiverKeyword)){
            List<Integer> list=customerAddrService.getids(receiverKeyword,receiverKeyword);
            wrapper.in("customer_addr_id",list);
        }
        if (StringUtils.isNotBlank(createTime)){
            wrapper.between("create_time", LocalDate.parse(createTime),LocalDate.parse(createTime).plusDays(1));
        }
        if (status!=null){
            wrapper.eq("order_status",status);
        }
        Page<OrderMaster> page=orderMasterService.page(new Page<>(pageNum,pageSize),wrapper);
        return new Result(true,200,"查询成功",page);
    }
    @GetMapping("/{oid}")
    @ApiOperation("查看订单详情")
    public Result detail(@ApiParam(value = "订单ID",required = false) @PathVariable("oid") Integer oid){
        OrderMaster orderMaster=orderMasterService.getById(oid);
        orderMaster.setAddr(customerAddrService.detail(orderMaster.getCustomerAddrId()));
        QueryWrapper<OrderDetail> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id",orderMaster.getOrderId());
        orderMaster.setOrderDetails(orderDetailService.list(wrapper));
        return new Result(true,200,"查询成功",orderMaster);
    }
    @PostMapping("/update/note")
    @ApiOperation("更新备注")
    public Result updateNote(@ApiParam(required = true,value = "备注内容") @RequestParam String note,
                             @ApiParam(required = true,value = "备注内容") @RequestParam Integer id){
        UpdateWrapper<OrderMaster> wrapper=new UpdateWrapper<>();
        wrapper.eq("order_id",id);
        wrapper.set("note",note);
        orderMasterService.update(wrapper);
        return detail(id);//执行成功后返回新的订单数据

    }

}
